package com.ws.haungjia.controller;

import com.ws.haungjia.Bean.Fushutu;
import com.ws.haungjia.Bean.luxian;
import com.ws.haungjia.Service.LvxingService;
import com.ws.haungjia.utils.Res;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/fushu")
public class FushuConfig {
    @Autowired
    private LvxingService lvxingService;

    @RequestMapping("/chaxun")
    public String chaxun(int id, Model model){
        List<Fushutu> fushutu = lvxingService.chafushu(id);
        model.addAttribute("id",id);
        model.addAttribute("fushutu",fushutu);
        return "WEB-INF/houtai/Fushutu/List";
    }



    @RequestMapping("/zengjia")
    public String zengjia(Fushutu fushutu,MultipartFile tu) throws IOException {

        if (tu.getSize() > 0) {
            String replace = UUID.randomUUID().toString().replace("-", "");
            String fileName = replace + "." + tu.getOriginalFilename().split("\\.")[1];
            fushutu.setTupian(fileName);
//            lastIndexo
            tu.transferTo(new File(Res.fileDir + fileName));
        }else{
            fushutu.setTupian("");
        }
        System.out.println(fushutu);
        int fushuzeng = lvxingService.fushuzeng(fushutu);
        if (fushuzeng > 0) {
            return "redirect:/fushu/chaxun?id="+fushutu.getPackageID();
        }else{
            return "WEB-INF/houtai/Fushutu/Zengjia";
        }
    }


    @RequestMapping("/shan")
    public String shan(int id,int packageID){
        int fushushan = lvxingService.fushushan(id);
        if (fushushan > 0) {
            return "redirect:/fushu/chaxun?id="+packageID;
        }else{
            return "redirect:/fushu/chaxun?id="+packageID;
        }
    }

    @RequestMapping("/tiaozhuan")
    public String tiaozhuan(int packageID, Model model){
        model.addAttribute("id",packageID);
        return "WEB-INF/houtai/Fushutu/zengjia";
    }
}
